﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Reflection;

namespace Might.UIControls.PropertyEditor.Items {
    public partial class EnumItem : UserControl , IPropertyEditorItemWithValue<Enum> {

        private string label;
        private Enum value;
        private bool raiseEvents = false;
        private Type enumType;
        private Dictionary<string , ComboBoxItem> comboBoxItems = new Dictionary<string , ComboBoxItem>();

        public EnumItem( string label , Type enumType ) {

            InitializeComponent();
            this.label = label;
            labelTxt.Text = label + " : ";
            this.enumType = enumType;                     

            var fields = from field in enumType.GetFields()
                         where field.IsLiteral
                         select field;

            foreach( FieldInfo field in fields ) {                            

                string enumValue = field.GetValue( enumType ).ToString();                                                              
                
                ComboBoxItem item = new ComboBoxItem();
                item.Content = new TextBlock() { Text = enumValue };
                item.Margin = new Thickness( 15 , 0 , 0 , 0 );
                item.Width = 100;
                item.Tag = enumValue;
                
                enumComboBox.Items.Add( item );
                
                comboBoxItems.Add( enumValue , item );
                
            }

            enumComboBox.SelectionChanged += new SelectionChangedEventHandler( enumComboBox_SelectionChanged );
            
        }

        public event EventHandler ValueChanged;

        public string Label { get { return label; } }

        public Enum Value {
            get {
                return value;
            }
            set {
                raiseEvents = false;
                this.value = value;
                if( value == null ) {
                    enumComboBox.SelectedItem = null;
                } else {
                    enumComboBox.SelectedItem = comboBoxItems[ value.ToString() ];
                }
                raiseEvents = true;
            }
        }

        protected void onValueChanged( EventArgs e ) {
            if( ValueChanged != null && raiseEvents ) {
                if( enumComboBox.SelectedItem == null ) { 
                    value = null;
                } else {
                    value = Enum.Parse( enumType , ( enumComboBox.SelectedItem as ComboBoxItem ).Tag as string , true ) as Enum;
                }
                ValueChanged( this , e );
            }
        }

        void enumComboBox_SelectionChanged( object sender , SelectionChangedEventArgs e ) {
            onValueChanged( null );
        }

    }
}
